package com.ww.microrpc.loadbalancer;

import com.ww.microrpc.client.Client;
import com.ww.microrpc.netty.NettyClientConfig;
import com.ww.microrpc.netty.RequestHolder;
import com.ww.microrpc.netty.protocol.ResponseMessage;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;

public class LoadBalancerClient implements Client {

    private CachingSpringLoadBalancerFactory lbClientFactory;

    private SpringClientFactory clientFactory;


    public LoadBalancerClient(CachingSpringLoadBalancerFactory lbClientFactory,
                              SpringClientFactory clientFactory,
                              NettyClientConfig nettyClientConfig) {
        this.lbClientFactory = lbClientFactory;
        this.clientFactory = clientFactory;
    }

    private RpcLoadBalancer lbClient(String clientName) {
        return this.lbClientFactory.create(clientName);
    }

    @Override
    public ResponseMessage execute(RequestHolder request) {
        String clientName = request.getClientName();
        return lbClient(clientName).executeFuture(request);
    }


//    IClientConfig getClientConfig(Request.Options options, String clientName) {
//        IClientConfig requestConfig;
//        if (options == DEFAULT_OPTIONS) {
//            requestConfig = this.clientFactory.getClientConfig(clientName);
//        }
//        else {
//            requestConfig = new MicroRpcOptionsClientConfig(options);
//        }
//        return requestConfig;
//     }

//    private NettyClient client;

//    @PostConstruct
//    public void init() {
//        this.client = new NettyClient("127.0.0.1", 28777);
//        this.client.start();
////        this.client.sendMessage("hello ww");
//    }

//    public RpcFuture executeFuture(RpcRequest request) {
////        String msg = "ww 123";
//        return client.sendMessage(request);
//    }


}
